package com.ruoyi.medication.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.medication.domain.Medication;
import com.ruoyi.medication.service.IMedicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 药物Controller
 * 
 * @author ruoyi
 * @date 2025-04-12
 */
@RestController
@RequestMapping("/medication/medication")
public class MedicationController extends BaseController {
    @Autowired
    private IMedicationService medicationService;

    /**
     * 查询药物列表
     */
    @PreAuthorize("@ss.hasPermi('medication:medication:list')")
    @GetMapping("/list")
    public TableDataInfo list(Medication medication) {
        startPage();
        List<Medication> list = medicationService.selectMedicationList(medication);
        return getDataTable(list);
    }

    /**
     * 导出药物列表
     */
    @PreAuthorize("@ss.hasPermi('medication:medication:export')")
    @Log(title = "药物", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Medication medication) {
        List<Medication> list = medicationService.selectMedicationList(medication);
        ExcelUtil<Medication> util = new ExcelUtil<Medication>(Medication.class);
        util.exportExcel(response, list, "药物数据");
    }

    /**
     * 获取药物详细信息
     */
    @PreAuthorize("@ss.hasPermi('medication:medication:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(medicationService.selectMedicationById(id));
    }

    /**
     * 新增药物
     */
    @PreAuthorize("@ss.hasPermi('medication:medication:add')")
    @Log(title = "药物", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Medication medication) {
        return toAjax(medicationService.insertMedication(medication));
    }

    /**
     * 修改药物
     */
    @PreAuthorize("@ss.hasPermi('medication:medication:edit')")
    @Log(title = "药物", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Medication medication) {
        return toAjax(medicationService.updateMedication(medication));
    }

    /**
     * 删除药物
     */
    @PreAuthorize("@ss.hasPermi('medication:medication:remove')")
    @Log(title = "药物", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(medicationService.deleteMedicationByIds(ids));
    }

    /**
     * 检查库存警告
     */
    @GetMapping("/checkStockWarning")
    public AjaxResult checkStockWarning() {
        List<Medication> medications = medicationService.selectMedicationList(new Medication());
        List<AjaxResult> warnings = medications.stream()
                .filter(medication -> medication.getStock() < 100)
                .map(medication -> {
                    return AjaxResult.success(
                            "药品【" + medication.getName() + "】当前库存为" + medication.getStock() + "，低于预警阈值100，请及时补充库存。");
                })
                .collect(Collectors.toList());
        return success(warnings);
    }
}
